<?php

use think\facade\Db;
use think\migration\Migrator;

class JoyshopVersion100Data extends Migrator
{
    public int $time = 0;

    public function up()
    {
        $this->time = time();

        $this->insertSettingData();
        $this->insertDeliveryCompanyData();
        $this->insertGoodsServiceData();

        $this->updateArea();
    }

    public function insertSettingData(): void
    {
        $table = $this->table('shop_setting');

        $data = [
            // 商城配置
            [
                'name'    => 'basic_group', 'group' => 'basic_group', 'title' => '', 'tip' => '', 'type' => 'array',
                'value'   => '[{"key":"basic","value":"基本配置"},{"key":"shop","value":"商城配置"}]',
                'content' => '', 'rule' => 'required', 'extend' => '', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'api_url', 'group' => 'basic', 'title' => 'API请求域名', 'tip' => '', 'type' => 'string',
                'value'   => 'http://localhost',
                'content' => '', 'rule' => 'required', 'extend' => '{"blockHelp":"配置项修改后需同步修改客户端的api请求域名"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'api_name', 'group' => 'basic', 'title' => 'API应用名', 'tip' => '', 'type' => 'string',
                'value'   => '/shop',
                'content' => '', 'rule' => 'required', 'extend' => '{"blockHelp":"配置项修改后需同步修改客户端的api应用名"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'auto_cancel_time', 'group' => 'shop', 'title' => '自动取消时间', 'tip' => '', 'type' => 'number',
                'value'   => '24',
                'content' => '', 'rule' => 'required,number', 'extend' => '{"blockHelp":"未支付订单【{}】小时后由系统自动取消","baInputExtend":{"min":1,"max":48}}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'auto_receive_time', 'group' => 'shop', 'title' => '自动收货时间', 'tip' => '', 'type' => 'number',
                'value'   => '7',
                'content' => '', 'rule' => 'required,number', 'extend' => '{"blockHelp":"已发货订单【{}】天后由系统自动确认收货","baInputExtend":{"min":1}}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'auto_evaluate_time', 'group' => 'shop', 'title' => '自动评价时间', 'tip' => '', 'type' => 'number',
                'value'   => '7',
                'content' => '', 'rule' => 'required,number', 'extend' => '{"blockHelp":"已收货订单【{}】天后未评价由系统自动完成评价","baInputExtend":{"min":1}}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'apply_refund_time', 'group' => 'shop', 'title' => '申请售后时间', 'tip' => '', 'type' => 'number',
                'value'   => '7',
                'content' => '', 'rule' => 'required,number', 'extend' => '{"blockHelp":"订单收货后，用户在【{}】天内可申请售后","baInputExtend":{"min":1}}', 'weigh' => 0, 'allow_del' => 0
            ],
            // 小程序配置
            [
                'name'    => 'miniapp_group', 'group' => 'miniapp_group', 'title' => '', 'tip' => '', 'type' => 'array',
                'value'   => '[{"key":"miniapp_config","value":"基本配置"},{"key":"miniapp_push","value":"消息推送配置"}]',
                'content' => '', 'rule' => 'required', 'extend' => '', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'miniapp_id', 'group' => 'miniapp_config', 'title' => '小程序AppID', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => 'required', 'extend' => '{"blockHelp":"微信小程序平台-开发-开发管理-开发设置，记录AppID (小程序ID)"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'miniapp_secret', 'group' => 'miniapp_config', 'title' => '小程序AppSecret', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => 'required', 'extend' => '{"blockHelp":"小程序平台-开发-开发管理-开发设置，记录AppSecret (小程序密钥)"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'miniapp_push_url', 'group' => 'miniapp_push', 'title' => '消息推送URL(服务器地址)', 'tip' => '根据API请求域名自动生成', 'type' => 'string',
                'value'   => '',
                'content' => '', 'rule' => '', 'extend' => '{"baInputExtend":{"disabled":true}, "blockHelp":"微信公众平台-开发-开发管理-开发设置-消息推送，设置URL(服务器地址)"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'miniapp_push_token', 'group' => 'miniapp_push', 'title' => '消息推送Token(令牌)', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => '', 'extend' => '{"blockHelp":"微信公众平台-开发-开发管理-开发设置-消息推送，设置Token(令牌)"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'miniapp_push_aes_key', 'group' => 'miniapp_push', 'title' => '消息推送EncodingAESKey(消息加密密钥)', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => '', 'extend' => '{"blockHelp":"微信公众平台-开发-开发管理-开发设置-消息推送，设置EncodingAESKey(消息加密密钥)"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'miniapp_push_encrypt_method', 'group' => 'miniapp_push', 'title' => '消息推送加密方式', 'tip' => '微信公众平台-开发-开发管理-开发设置-消息推送，设置加密方式', 'type' => 'radio',
                'value'   => 'safe_mode',
                'content' => '{"safe_mode":"安全模式"}', 'rule' => '', 'extend' => '{"baInputExtend":{"border":true}}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'miniapp_push_data_format', 'group' => 'miniapp_push', 'title' => '消息推送数据格式', 'tip' => '微信公众平台-开发-开发管理-开发设置-消息推送，设置数据格式', 'type' => 'radio',
                'value'   => 'xml',
                'content' => '{"xml":"XML"}', 'rule' => '', 'extend' => '{"baInputExtend":{"border":true}}', 'weigh' => 0, 'allow_del' => 0
            ],
            // 协议配置
            [
                'name'    => 'agreement_group', 'group' => 'agreement_group', 'title' => '', 'tip' => '', 'type' => 'array',
                'value'   => '[{"key":"user","value":"用户协议"},{"key":"privacy","value":"隐私协议"},{"key":"about","value":"关于我们"}]',
                'content' => '', 'rule' => 'required', 'extend' => '{"baInputExtend":{"min":1}}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'user_agreement', 'group' => 'user', 'title' => '用户协议', 'tip' => '', 'type' => 'editor',
                'value'   => '<p>用户协议</p>',
                'content' => '', 'rule' => 'editorRequired', 'extend' => '', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'privacy_agreement', 'group' => 'privacy', 'title' => '隐私协议', 'tip' => '', 'type' => 'editor',
                'value'   => '<p>隐私协议</p>',
                'content' => '', 'rule' => 'editorRequired', 'extend' => '', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'about_us_agreement', 'group' => 'about', 'title' => '关于我们', 'tip' => '', 'type' => 'editor',
                'value'   => '<p>关于我们</p>',
                'content' => '', 'rule' => '', 'extend' => '', 'weigh' => 0, 'allow_del' => 0
            ],
            // 支付配置
            [
                'name'    => '', 'group' => 'pay_group', 'title' => '', 'tip' => '', 'type' => 'array',
                'value'   => '[{"key":"wxpay","value":"微信支付"}]',
                'content' => '', 'rule' => 'required', 'extend' => '', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'wxpay_app_id', 'group' => 'wxpay', 'title' => '应用ID(APPID)', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => 'required', 'extend' => '', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'wxpay_mch_id', 'group' => 'wxpay', 'title' => '商户号(MchId)', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => 'required', 'extend' => '{"blockHelp":"可在 https://pay.weixin.qq.com 账户中心-商户信息 查看"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'wxpay_public_cert', 'group' => 'wxpay', 'title' => '公钥证书路径', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => 'required', 'extend' => '{"blockHelp":"输入公钥证书（apiclient_cert.pem）在服务器上的绝对路径（注意权限）"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'wxpay_private_cert', 'group' => 'wxpay', 'title' => '私钥证书路径', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => 'required', 'extend' => '{"blockHelp":"输入私钥证书（apiclient_key.pem）在服务器上的绝对路径（注意权限）"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'wxpay_secret_key', 'group' => 'wxpay', 'title' => 'v3 API 秘钥', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => 'required', 'extend' => '{"blockHelp":"可在 账户中心-API安全 中设置"}', 'weigh' => 0, 'allow_del' => 0
            ],
            // 物流配置
            [
                'name'    => 'delivery_group', 'group' => 'delivery_group', 'title' => '', 'tip' => '', 'type' => 'array',
                'value'   => '[{"key":"delivery_config","value":"基本配置"},{"key":"kd100_group","value":"快递100配置"}]',
                'content' => '', 'rule' => 'required', 'extend' => '', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'delivery_api_provider', 'group' => 'delivery_config', 'title' => '物流查询API服务商', 'tip' => '小程序查询组件：小程序-功能-物流服务-快递服务-开通查询组件', 'type' => 'radio',
                'value'   => 'wechat',
                'content' => '{"wechat":"小程序查询组件","kd100":"快递100"}', 'rule' => 'required', 'extend' => '{"baInputExtend":{"border":true}}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'sync_delivery', 'group' => 'delivery_config', 'title' => '同步物流信息', 'tip' => '发货时自动同步发货信息到小程序发货管理', 'type' => 'switch',
                'value'   => '1',
                'content' => '', 'rule' => 'required', 'extend' => '{"baInputExtend":{"activeText":"启用","inactiveText":"禁用"}}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'kd100_key', 'group' => 'kd100_group', 'title' => '授权key', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => 'required', 'extend' => '{"blockHelp": "获取地址：https://api.kuaidi100.com/manager/v2/myinfo/enterprise"}', 'weigh' => 0, 'allow_del' => 0
            ],
            [
                'name'    => 'kd100_customer', 'group' => 'kd100_group', 'title' => '授权customer', 'tip' => '', 'type' => 'password',
                'value'   => '',
                'content' => '', 'rule' => 'required', 'extend' => '{"blockHelp": "申请企业版获取，申请地址：https://api.kuaidi100.com/register"}', 'weigh' => 0, 'allow_del' => 0
            ],
        ];

        $existData = Db::name($table->getName())->select()->toArray();
        foreach ($data as $key => $item) {
            if (in_array($item['name'], array_column($existData, 'name'))) {
                unset($data[$key]);
            }
            if ($item['name'] == $item['group'] && in_array($item['group'], array_column($existData, 'group'))) {
                unset($data[$key]);
            }
        }

        if ($data) {
            $table->insert($data)->saveData();
        }
    }

    public function insertDeliveryCompanyData(): void
    {
        $table = $this->table('shop_delivery_company');

        $data = [
            ['name' => '圆通速递', 'code_kd100' => 'yuantong', 'code_kdniao' => 'YTO', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
            ['name' => '申通快递', 'code_kd100' => 'shentong', 'code_kdniao' => 'STO', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
            ['name' => '极兔速递', 'code_kd100' => 'jtexpress', 'code_kdniao' => 'JTSD', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
            ['name' => '中通快递', 'code_kd100' => 'zhongtong', 'code_kdniao' => 'ZTO', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
            ['name' => '韵达快递', 'code_kd100' => 'yunda', 'code_kdniao' => 'YD', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
            ['name' => '邮政快递', 'code_kd100' => 'youzhengguonei', 'code_kdniao' => 'YZPY', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
            ['name' => '顺丰速运', 'code_kd100' => 'shunfeng', 'code_kdniao' => 'SF', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
            ['name' => '京东快递', 'code_kd100' => 'jd', 'code_kdniao' => 'JD', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
        ];

        $table->insert($data)->saveData();
    }

    public function insertGoodsServiceData(): void
    {
        $table = $this->table('shop_goods_service');

        $data = [
            ['name' => '无忧退货', 'content' => '自收到商品之日起30天内，可在线申请无忧退货服务（部分特殊商品除外）', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
            ['name' => '快速退款', 'content' => '收到退货包裹并确认无误后，将在48小时内办理退款，退款将原路返回，预计1-5个工作日到账。', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
            ['name' => '七天无理由退换', 'content' => '满足相应的条件后(使用后不支持)，消费者可选择七天无理由退换货', 'status' => 1, 'create_time' => $this->time, 'update_time' => $this->time],
        ];

        $existData = Db::name($table->getName())->select()->toArray();

        if (empty($existData)) {
            $table->insert($data)->saveData();

        }
    }

    public function updateArea()
    {
        if ($this->hasTable('area')) {
            $table = $this->table('area');

            if (!$table->hasIndex('pid')) {
                $table->addIndex(['pid'], ['type' => 'BTREE'])->update();
            }
            if (!$table->hasIndex('level')) {
                $table->addIndex(['level'], ['type' => 'BTREE'])->update();
            }
        }
    }
}
